package com.beiye.drivertransport.SubActivity;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.adapter.LearnPagerAdapter;
import com.beiye.drivertransport.fragment.CourseSupermarketHomeFragment;
import com.beiye.drivertransport.fragment.SpecifiedLearningHomeFragment;
import com.githang.statusbar.StatusBarCompat;

import java.lang.reflect.Field;
import java.util.ArrayList;


//驾驶员安全学习列表
public class SubSafeLearnActivity extends TwoBaseAty {
//    @Bind(R.id.btn_first)
//    TextView leanrnbtn;
//    @Bind(R.id.btn_second)
//    TextView leanrnbtn2;

      TabLayout TabLayout1;
    private  int TAB_MARGIN_DIP = 35;

    ViewPager myviewpager;
//    //所有标题按钮的数组
//    private TextView[] btnArgs;
    private ArrayList<Fragment> fragments;
    private LearnPagerAdapter adapter;
    private ArrayList<String> list_title;


    @Override
    public int getLayoutId() {
        return R.layout.activity_sub_safe_learn;

    }

    @Override
    public void initData() {
        TabLayout1 = (TabLayout) findViewById(R.id.tab_FindFragment_title1);
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);

        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#1F80C4"));
        Bundle bundle = getIntent().getExtras();
        int currentItem= bundle.getInt("currentItem");
        int minPer = bundle.getInt("minPer");
        int readInterval = bundle.getInt("readInterval");
        int signMark = bundle.getInt("signMark");
        int Mark = bundle.getInt("Mark");
        int photoMark = bundle.getInt("photoMark");
        String orgId = bundle.getString("orgId");
        String orgName = bundle.getString("orgName");

//        LinearLayout le_botton =(LinearLayout) findViewById(R.id.bottonle);
//        le_botton.setOnClickListener(null);//解决导航顶部上面点击事件跳转首页广告详情页界面bug，应该不能跳转
        ImageView img_back = (ImageView)findViewById(R.id.img_safeback);
        //返回首页
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

//        //初始化按钮数组
//        btnArgs = new TextView[]{leanrnbtn,leanrnbtn2};
//
//        //先重置所有按钮颜色
//        resetButtonColor();
//        //再将第一个按钮字体设置为红色，表示默认选中第一个
//        leanrnbtn.setTextColor(Color.WHITE);
//        leanrnbtn.setBackgroundResource(R.drawable.learnpressbtn);
        fragments = new ArrayList<Fragment>();
        SpecifiedLearningHomeFragment    specifiedLearningHomeFragment = new SpecifiedLearningHomeFragment();
        fragments.add(specifiedLearningHomeFragment);
        CourseSupermarketHomeFragment courseSupermarketHomeFragment = new CourseSupermarketHomeFragment();
        fragments.add(courseSupermarketHomeFragment);

        Bundle args1 = new Bundle();
        args1.putString("orgName", orgName);
        args1.putString("orgId", orgId);
        args1.putInt("minPer", minPer);
        args1.putInt("readInterval", readInterval);
        args1.putInt("signMark", signMark);
        args1.putInt("Mark", Mark);
        args1.putInt("photoMark", photoMark);
        specifiedLearningHomeFragment.setArguments(args1);
        Bundle args2 = new Bundle();
        args2.putString("orgName", orgName);
        args2.putString("orgId", orgId);
        args2.putInt("minPer", minPer);
        args2.putInt("readInterval", readInterval);
        args2.putInt("signMark", signMark);
        args2.putInt("Mark", Mark);
        args2.putInt("photoMark", photoMark);
        courseSupermarketHomeFragment.setArguments(args2);






        //将名称加载tab名字列表，正常情况下，我们应该在values/arrays.xml中进行定义然后调用
        list_title = new ArrayList<>();
        list_title.add("指定学习");
        list_title.add("课程超市");
        TabLayout1.setTabMode(TabLayout.MODE_FIXED);
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(0)));
        TabLayout1.addTab(TabLayout1.newTab().setText(list_title.get(1)));
        adapter = new LearnPagerAdapter(getSupportFragmentManager(),fragments,list_title);
        myviewpager.setAdapter(adapter);
        TabLayout1.setupWithViewPager(myviewpager);

        if(currentItem==0)
        {
            myviewpager.setCurrentItem(0);
            TabLayout1.getTabAt(0).select();
        }else if(currentItem==1)
        {
            myviewpager.setCurrentItem(1);
            TabLayout1.getTabAt(1).select();
        }
        setIndicator(SubSafeLearnActivity.this, TabLayout1, TAB_MARGIN_DIP, TAB_MARGIN_DIP);


    }
//修改tablayout的tabIndicator宽度
    public  void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field slidingTabIndicator = null;
        try {
            slidingTabIndicator = tabLayout.getDeclaredField("slidingTabIndicator");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        slidingTabIndicator.setAccessible(true);
        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) slidingTabIndicator.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }
    public  DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    public  float getPXfromDP(float value, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics());
    }
    @Override
    public void requestData() {

    }
}
